<?php
namespace app\web\model;
use think\Db;
use think\Model;

class Web extends Model{

    //获取文章分类
    function getArticle($catid=0,$offset=5) {
        $pid = getCategoryAll($catid);
        $result = Db::name('article')
            ->field('id,title,summary,pid,thumb,hot,created_time')
            ->where('pid','in',$pid)
            ->order('created_time','desc')
            ->limit($offset)
            ->select();
        return $result;
    }


    //获取图片热点新闻
    function getImageNews($catid=0,$offset=4) {
        $pid = getCategoryAll($catid);
        $result = Db::name('article')
            ->field('id,title,summary,pid,thumb,hot,created_time')
            ->where('hot',1)
            ->where('thumb','not null')
            ->where('pid','in',$pid)
            ->limit($offset)
            ->select();
        return $result;
    }

    //获取轮播图
    function getSlider($offset = 4){
        $result = Db::name('slider')
            ->order('sort')
            ->limit($offset)
            ->select();
        return $result;
    }

    //获取指定分类信息及子分类信息
    function getCate($catid){
        $pid_array = getCategoryAll($catid);
        $temp = Db::name('category')
            ->where('pid','in',$pid_array)
            ->select();
        $result = [];
        $result['child'] = [];
        foreach($temp as $key=>$it){
             if($it['pid'] == $catid) {
                 $result['current'] = $it;
             }else{
                 $result['child'][] = $it;
             }
        }
        return $result;
    }

    //获取分类列表内容返回分页模式数据
    function getCatelist($catid,$page=15,$query=[]){
        $pid_array = getCategoryAll($catid);
        $result = Db::name('article')
            ->field('id,title,pid,created_time,hot,top')
            ->where('pid','in',$pid_array)
            ->paginate($page,false,['query'=>$query]);

        return $result;
    }

    //获取指定文章
    function getArticleOne($id){
        $result = Db::name('article')
            ->find($id);
        return $result;
    }

    //获取指定文章ID，相关的如同一分类下页的其它文章
    function getSimilarArticle($id,$offset = 5){
        $pid        = Db::name('article')->where('id',$id)->value('pid');
        $pid_array  = getCategoryAll($pid);
        $article    = Db::name('article')
            ->where('pid','in',$pid_array)
            ->where('id','<>',$id)
            ->limit($offset)
            ->select();
        return $article;
    }


}